<!--
* description: 新增编辑-门店级别系数
* author:wangjuan
* createdDate:2020-12-4
-->
<template>
  <section class="editBigAreaInfo">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      width="70%"
      @close="closeEdit"
      @opened="open"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :style-type="$store.state.app.dialogType"
      />

      <div class="filter-container filter-params">
        <el-row ref="vailComponentList" :gutter="26">

          <one-table-template
            ref="multipleTable"
            :dynamic-buttons="tableButtons"
            :dynamic-components="tableComponents"
            :dynamic-api-config="apiConfig"
            :dynamic-form-fields="formField"
            :dynamic-is-show-select="true"
            :dynamic-is-show-more-btn="true"
            :dynamic-is-init-table="true"
          />
        </el-row>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from '@/components/mixins/formMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import { vehiclePdiPriceInquirys } from '@/api/se/vehiclePdiPriceInquiry'

export default {
  components: {
    dialogHeader: () => import('@/components/dialogHeader'),
    OneTableTemplate
  },

  // 组件混入对象
  mixins: [formMixins],

  props: {
    popupsVisible: { type: Boolean, default: false },
    // 从网格传入的编辑行数据(json)
    dynamicEditRowData: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          icon: 'iconfont  icon-preservation',
          clickEvent: () => this.save(),
          text: this.$t('sys.button.save') /* 保存*/,
          position: 'right'
        },
        {
          compKey: 'btnKey2',
          type: '',
          size: 'small',
          icon: 'iconfont  icon-cancel',
          clickEvent: () => this.close(),
          text: this.$t('sys.button.cancel') // 取消
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '授权内容说明：',
              codeField: 'remark',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isRequire: true,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '效验类型：',
              codeField: 'preType',
              component: () => import('@/components/org/commonSelect'),
              customObj: {
                api: vehiclePdiPriceInquirys.queryType
              },
              type: 'dropdownList',
              isRequire: true,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '最低效验值：',
              codeField: 'partSum',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '效验（配件编码/工时代码）：',
              codeField: 'preCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '效验（配件名称/维修内容）：',
              codeField: 'preName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            }
          ],

      // 标题
      textMap: {
        edit: '门店级别系数维护',
        add: '门店级别系数维护'
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: this.$utils.getFormField(
        this,
        {
          remark: '', // 授权内容说明
          preType: '', // 效验类型
          preCode: '', // 工效验(配件编码/工时代码)
          preName: '', // 效验(配件名称/维修内容)
          partSum: '',
          preRuleId: ''
        },
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
      )
    }
  },

  methods: {
    closeEdit() {
      this.$emit('close')
      this.$emit('update:curPopupsVisible', false)
    },
    // 新增/编辑保存按钮
    save() {
      console.log(this.popupsState, 6666)
      this.$utils.validataMoth(this, 'validpopup')
      if (this.valid) {
        if (this.popupsState === 'edit') {
          this.editSave()
        } else {
          this.addSave()
        }
      }
    },
    // 编辑保存
    editSave() {
      const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: {
          APIUrl: '/mp/e3sh/aftersale/preauthorizationservice/UpAdd.do'
        },
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: that.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then((response) => {
        if (response.result === '1') {
          this.$message({
            message: this.$t('sys.tips.esTip5') /* 保存成功*/,
            type: 'success',
            duration: 1000
          })
          this.curPopupsVisible = false
          this.$nextTick(() => {
            this.$parent.queryTable(1)
          })
        } else {
          // 保存失败
          this.$message({
            message: '保存失败:' + response.msg,
            type: 'warn',
            uration: 1000
          })
        }
        this.curPopupsVisible = false
      })
    },
    // 新增保存
    addSave() {
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: {
          APIUrl: '/mp/e3sh/aftersale/preauthorizationservice/Addl.do'
        },
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: this.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then((response) => {
        if (response.result === '1') {
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1000
          })
          this.curPopupsVisible = false
          this.$nextTick(() => {
            this.$parent.queryTable(1)
          })
        } else {
          // 保存失败
          this.$message({
            message: '添加失败:' + response.msg,
            type: 'warn',
            uration: 1000
          })
        }
        this.curPopupsVisible = false
      })
    },
    open() {
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: {
          APIUrl: '/mp/e3sh/aftersale/preauthorizationservice/queryType.do'
        },
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {}
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then((response) => {})
      if (this.popupsState === 'edit') {
        this.formField.remark = this.dynamicEditRowData.remark
        this.formField.preType = this.dynamicEditRowData.preType
        this.formField.preCode = this.dynamicEditRowData.preCode
        this.formField.preName = this.dynamicEditRowData.preName
        this.formField.partSum = this.dynamicEditRowData.partSum
        this.formField.preRuleId = this.dynamicEditRowData.preRuleId
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.promptSpan {
  position: absolute;
  bottom: 26px;
  right: 335px;
  color: #e70020;
}
</style>

